//
//  LoginService.m
//  HxgProject
//
//  Created by hxg on 2018/3/25.
//  Copyright © 2018年 XMYY-19. All rights reserved.
//

#import "LoginService.h"
#import "AFHTTPSessionManager.h"
#import "HxgInterfaceDefine.h"
#import "NetworkManager.h"
#import "ResObject.h"

#import "UserInfo.h"

@implementation LoginService

- (void)getVerifyCodeByPhone:(NSString *)phoneNum withCallBack:(void(^)(ResObject *resObj)) callBack
{
    ResObject *resObject = [[ResObject alloc] init];
    
    NetworkManager *man = [NetworkManager sharedNetworkManager];
    NSString *urlInfo = [NSString stringWithFormat:@"%@%@",iP_Address,JK_Reg_kGetVerifyCode];
    //2.封装参数
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"phone"] = phoneNum;
    [man postURL:urlInfo parameters:params success:^(id responseObject) {
        NSLog(@"responseObject:%@",responseObject);

        [[NSUserDefaults standardUserDefaults] setObject:phoneNum
                                                  forKey:kPhoneNum];
        
        if ([[responseObject objectForKey:kSuccess]  isEqual: @(YES)])
        {
            resObject.isSuccess = YES;
            callBack(resObject);
        }
        else
        {
            resObject.isSuccess = NO;
            resObject.errMsg = [responseObject objectForKey:@"errMsg"];
            callBack(resObject);
        }
        
        
    } failure:^(NSError *error, ParamtersJudgeCode judgeCode) {
        
        NSLog(@"judgeCode===>%lu",(unsigned long)judgeCode);
        
        resObject.isSuccess = NO;
        resObject.errMsg = @"获取验证码失败!";
        callBack(resObject);
    }];
}

- (void)registerByPhone:(NSString *)phoneNum
                     withPsd:(NSString *)psd
              withVerifyCode:(NSString *)verifyCode
                withCallBack:(void(^)(ResObject *)) callBack
{
    NetworkManager *man = [NetworkManager sharedNetworkManager];
    NSString *urlInfo = [NSString stringWithFormat:@"%@%@",iP_Address,JK_Reg_kUsrRegister];
    //2.封装参数
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"phone"] = phoneNum;
    params[@"loginPwd"] = psd;
    params[@"verifyCode"] = verifyCode;

    ResObject *myRes = [[ResObject alloc] init];
    
    [man postURL:urlInfo parameters:params success:^(id responseObject) {
        NSLog(@"responseObject:%@",responseObject);
    
    if ([[responseObject objectForKey:kSuccess]  isEqual: @(YES)])
        {
            myRes.isSuccess = YES;
            callBack(myRes);
        }
        else
        {
            myRes.isSuccess = NO;
            myRes.errMsg = [responseObject objectForKey:@"errMsg"];
            callBack(myRes);
        }
        
    } failure:^(NSError *error, ParamtersJudgeCode judgeCode) {
        NSLog(@"judgeCode===>%lu",(unsigned long)judgeCode);
        
        myRes.isSuccess = NO;
        myRes.errMsg = @"操作失败!";
        callBack(myRes);
    }];
}

- (void)resetPsw:(NSString *)phoneNum
      withnewPwd:(NSString *)newPwd
  withVerifyCode:(NSString *)verifyCode
    withCallBack:(void(^)(ResObject *)) callBack
{
    NetworkManager *man = [NetworkManager sharedNetworkManager];
    NSString *urlInfo = [NSString stringWithFormat:@"%@%@",iP_Address,JK_Reg_kResetPassword];
    //2.封装参数
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"phone"] = phoneNum;
    params[@"loginPwd"] = newPwd;
    params[@"verifyCode"] = verifyCode;
    
    ResObject *myRes = [[ResObject alloc] init];
    [man postURL:urlInfo parameters:params success:^(id responseObject) {
        NSLog(@"responseObject:%@",responseObject);
        
        if ([[responseObject objectForKey:kSuccess]  isEqual: @(YES)])
        {
            myRes.isSuccess = YES;
            callBack(myRes);
        }
        else
        {
            myRes.isSuccess = NO;
            myRes.errMsg = [responseObject objectForKey:@"errMsg"];
            callBack(myRes);
        }
        
    } failure:^(NSError *error, ParamtersJudgeCode judgeCode) {
        NSLog(@"judgeCode===>%lu",(unsigned long)judgeCode);
        myRes.isSuccess = NO;
        myRes.errMsg = @"操作失败!";
        callBack(myRes);
    }];
}

- (void)loginWithPhone:(NSString *)phoneNum
                   pwd:(NSString *)newPwd
          withCallBack:(void(^)(ResObject *res,UserInfo *userInfo)) callBack
{
    NetworkManager *man = [NetworkManager sharedNetworkManager];
    NSString *urlInfo = [NSString stringWithFormat:@"%@%@",iP_Address,JK_Login_kPhoneLogin];
    //2.封装参数
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"phone"] = phoneNum;
    params[@"loginPwd"] = newPwd;
    
    ResObject *myRes = [[ResObject alloc] init];
    
    [man postURL:urlInfo parameters:params success:^(id responseObject) {
        NSLog(@"responseObject:%@",responseObject);
        
        if ([[responseObject objectForKey:kSuccess]  isEqual: @(YES)])
        {
            UserInfo *userInfo = [[UserInfo alloc] initWithDic:responseObject[@"appLoginInfo"]];
            myRes.isSuccess = YES;
            callBack(myRes,userInfo);
        }  else {
            myRes.isSuccess = NO;
            myRes.errMsg = [responseObject objectForKey:@"errMsg"];
            callBack(myRes,nil);
        }
        
    } failure:^(NSError *error, ParamtersJudgeCode judgeCode) {
        NSLog(@"judgeCode===>%lu",(unsigned long)judgeCode);
        myRes.isSuccess = NO;
        myRes.errMsg = @"操作失败!";
        callBack(myRes,nil);
    }];
}


@end
